package org.example;

import java.time.LocalDateTime;
import java.util.Scanner;

class BookDAO extends BaseDAO<Book> {
    public BookDAO() {
        super(Book.class);
    }

    @Override
    public void insert(Book entity) throws Exception {
        if (entity == null) {
            throw new IllegalArgumentException("图书实体不能为空");
        }
        super.insert(entity);
    }

    public void insert(Scanner scanner) throws Exception {
        if (scanner == null) {
            throw new IllegalArgumentException("Scanner对象不能为空");
        }

        System.out.println("==zqw=== 请输入图书信息 ==zqw===");
        Book book = new Book();

        System.out.print("请输入书本编号：");
        book.setBook_id(scanner.nextLine().trim());

        System.out.print("请输入书名：");
        book.setTitle(scanner.nextLine().trim());

        System.out.print("请输入作者：");
        book.setAuthor(scanner.nextLine().trim());

        while (true) {
            try {
                System.out.print("请输入出版年份：");
                int year = Integer.parseInt(scanner.nextLine().trim());
                int currentYear = LocalDateTime.now().getYear();
                if (year > 1000 && year <= currentYear) {
                    book.setPublish_year(year);
                    break;
                } else {
                    System.out.println("出版年份必须在1001到" + currentYear + "之间");
                }
            } catch (NumberFormatException e) {
                System.out.println("出版年份输入有误，请输入数字");
            }
        }

        this.insert(book);
        System.out.println("图书信息插入成功");
    }
}
